<?php

/**
 * uniBoard Editor
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_editor{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Editor Templates
	 *
	 * @var object
	 */
	
	private $template		= NULL;
	
	/**
	 * Editor Configuration
	 *
	 * @var string
	 */
	
	private $editor_config	= array();
	
	/**
	 * Textfield id and name
	 *
	 * @var string
	 */
	
	private $editor_name	= NULL;
	
	/**
	 * Textfield value
	 *
	 * @var string
	 */
	
	private $editor_value	= NULL;
	
	/**
	 * Editor Data
	 *
	 * @var string
	 */
	
	private $editor_data	= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 * @param string $name
	 */
	
	function __construct( $appCore, $name, $value = ''){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Template
		$this -> template = new tpl_editor( $this -> appCore);
		
		// Set field name
		$this -> editor_name = $name;
		
		// Set field value
		$this -> editor_value = $value;
		
		// Load Editor Data
		$this -> editor_data = class_cache::loadCache('editor_data');
		
		// We have to build data ourself?
		if (class_cache::$load_error)
		{
			// Make empty editor data
			$this -> editor_data = array(
				0	=> array(), // Custom BBCodes
				1	=> 0, // Custom BBCodes Number
				2	=> array(), // GeSHi Languages
				3	=> 0, // GeSHi Languages Number
				4	=> array(), // Emoticons
				5	=> 0 // Emoticons Number
			);
			
			// Select BBCodes
			$appCore -> db -> query('SELECT bbcode_icon, bbcode_name, bbcode_tag, bbcode_type
			FROM ' . DB_PREFIX . 'bbcodes
			WHERE bbcode_system = \'0\' AND bbcode_in_editor = \'1\'
			ORDER BY bbcode_name');
			
			// Pass them to teh array
			while ( $bbcode_result = $appCore -> db -> fetch_assoc())
			{
				// Add to array
				$this -> editor_data[0][] = $bbcode_result;
				
				// Increase counter
				$this -> editor_data[1] ++;
			}
			
			// Free Result
			$appCore -> db -> freeResult();
			
			// Select GeSHi Data
			$appCore -> db -> query('SELECT geshi_name, geshi_tag
			FROM ' . DB_PREFIX . 'bbcodes_geshi
			ORDER BY geshi_name');
			
			// Pass them to teh array
			while ( $geshi_result = $appCore -> db -> fetch_assoc())
			{
				// Add to array
				$this -> editor_data[2][] = $geshi_result;
				
				// Increase counter
				$this -> editor_data[3] ++;
			}
			
			// Free Result
			$appCore -> db -> freeResult();
			
			// Select Smileys Data
			$appCore -> db -> query('SELECT smiley_replace, smiley_image, smiley_width, smiley_height, smiley_name
			FROM ' . DB_PREFIX . 'smileys
			WHERE smiley_in_editor = \'1\'
			ORDER BY smiley_name');
			
			// Pass them to teh array
			while ( $geshi_result = $appCore -> db -> fetch_assoc())
			{
				// Add to array
				$this -> editor_data[4][] = $geshi_result;
				
				// Increase counter
				$this -> editor_data[5] ++;
			}
			
			// Free Result
			$appCore -> db -> freeResult();
			
			// Save data
			class_cache::saveCache('editor_data', $this -> editor_data);
		}
		
	}
	
	function setConfig( $config_raw = array()){
		
		// Set Config
		$this -> editor_config = array(
			'bbcode' 	=> (isset($config_raw['bbcode']) && $config_raw['bbcode']),
			'smileys' 	=> (isset($config_raw['smileys']) && $config_raw['smileys']),
			'links' 	=> (isset($config_raw['links']) && $config_raw['links']),
			'geshi' 	=> (isset($config_raw['geshi']) && $config_raw['geshi']),
			'images'	=> (isset($config_raw['images']) && $config_raw['images']),
			'youtube' 	=> (isset($config_raw['youtube']) && $config_raw['youtube'])
		);
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws editor
	 * 
	 * @param bool $double
	 * @return string
	 */
	
	function draw( $full = true){
		
		// Template data
		$tpl_data = array(
			'images_dir'	=> EDITOR_ICONS_PATH,
			'name' 			=> $this -> editor_name,
			'value' 		=> $this -> editor_value,
			'geshi'			=> '',
			'smileys'		=> '',
			'url'			=> $this -> editor_config['links'],
			'img'			=> $this -> editor_config['images'],
			'yt'			=> $this -> editor_config['youtube']
		);
		
		// Draw GeSHi?
		if ( $this -> editor_config['geshi'] && $this -> editor_data[3] > 0)
		{
			for ( $i = 0; $i < $this -> editor_data[3]; $i ++)
			{
				$tpl_data['geshi'] .= $this -> template -> tpl_editor_geshi(array(
					'name'			=> $this -> editor_name,
					'geshi_name'	=>  htmlspecialchars($this -> editor_data[2][$i]['geshi_name']),
					'geshi_tag'		=>  htmlspecialchars($this -> editor_data[2][$i]['geshi_tag'])
				));
			}
		}
		
		// Draw Smileys?
		if ( $this -> editor_config['smileys'] && $this -> editor_data[5] > 0)
		{
			for ( $i = 0; $i < $this -> editor_data[5]; $i ++)
			{
				$tpl_data['smileys'] .= $this -> template -> tpl_editor_smiley(array(
					'name'				=> $this -> editor_name,
					'smiley_replace'	=> $this -> editor_data[4][$i]['smiley_replace'],
					'smiley_name'		=> $this -> editor_data[4][$i]['smiley_name'],
					'smiley_image'		=> SMILEYS_PATH . htmlspecialchars($this -> editor_data[4][$i]['smiley_image']),
					'smiley_width'		=> htmlspecialchars($this -> editor_data[4][$i]['smiley_width']),
					'smiley_height'		=> htmlspecialchars($this -> editor_data[4][$i]['smiley_height'])
				));
			}
		}
		
		// Draw full template?
		if ( $full)
		{
			// Draw Compact Template
			return $this -> template -> tpl_editor_body($tpl_data);
		}
		else
		{
			// Draw Full Template
			return $this -> template -> tpl_editor_compact($tpl_data);
		}
		
	}

	// ------------------------------------------------------------------------

	/**
	 * Draws editor options
	 * 
	 * @param array $whitelist
	 * @param array $options
	 * @return string
	 */
	
	function drawOptions($whitelist, $options){
		
		// Return any options?
		if (isset($whitelist))
		{
			// Draw template
			return $this -> template -> tpl_options(array(
				'name'				=> $this -> editor_name,
				'bbcodes'			=> in_array('bbcodes', $whitelist),
				'bbcodes_select'	=> !isset($options['bbcodes']) || $options['bbcodes'],
				'smileys'			=> in_array('bbcodes', $whitelist),
				'smileys_select'	=> !isset($options['smileys']) || $options['smileys'],
				'links'				=> in_array('bbcodes', $whitelist),
				'links_select'		=> !isset($options['links']) || $options['links'],
			));			
		}
		else
		{
			// Return empty string
			return NULL;
		}
		
	}
	
}